<?php
/**
 * Created by PhpStorm.
 * Author: 黑瞎子
 * Date: 2018/3/16
 * Time: 16:51
 */

namespace app\api\model;


class Theme extends BaseModel
{
    protected $hidden = ['topic_img_id','head_img_id','delete_time','update_time'];

    //关联Image，获取首页主题的图片
    public function topicImg(){
        return $this->belongsTo('Image', 'topic_img_id', 'id');
    }

    //关联Image，获取主题页头图的图片
    public function headImg(){
        return $this->belongsTo('Image', 'head_img_id', 'id');
    }

    //关联Product，获取单个主题下的产品列表
    public function products(){
        return $this->belongsToMany('Product','theme_product',
            'product_id','theme_id');
    }

    //获取主题信息
    public static function getThemeByIDs($ids){
        $result = self::with(['topicImg', 'headImg'])->select($ids);
        return $result;
    }

    //获取单个主题的产品信息
    public static function getThemeWithProduct($id){
        $theme = self::with(['products','topicImg','headImg'])->find($id);
        return $theme;
    }
}